package com.dididoctor.patient.Activity.Inquiry;

import android.content.Context;

import com.dididoctor.patient.Activity.ConstantValue;
import com.dididoctor.patient.Activity.Doctor.Doctordetail.Prdbuy;
import com.dididoctor.patient.Activity.Doctor.Doctordetail.Prdetail;
import com.dididoctor.patient.Bean.Token;
import com.dididoctor.patient.Http.MyHttpResponseHandler;
import com.dididoctor.patient.MV.BasePresenter;
import com.dididoctor.patient.MV.BusinessClient;
import com.dididoctor.patient.MV.Response;
import com.dididoctor.patient.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @version 2.0
 * @类描述：
 * @项目名称：ddgh
 * @类名称： InquiryPresenter
 * @包名称： com.dididoctor.patient.Activity.Inquiry
 * @创建人： zhanjunjie
 * @创建时间：2016/4/12 17:57
 * @修改人：
 * @修改时间：2016/4/12 17:57
 * @修改备注： new parent class(用一句话描述修改了什么)
 */
public class InquiryPresenter extends BasePresenter {

    private InquiryView view;

    public InquiryPresenter(Context context, InquiryView view) {
        super(context, view);
        this.view = view;
    }

    void getInquiry(String diseId, String doctorId) {
        RequestParams params = new RequestParams();
        params.put("diseId", diseId);
        params.put("doctorId", doctorId);
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.medicalqus, params, new MyHttpResponseHandler() {
            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();
            private List<Inquiry> inquiries = new ArrayList<Inquiry>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    maps = response.getListData("quslist");
                    for (Map<String, String> map : maps) {
                        Inquiry inquiry = new Inquiry();
                        inquiry.setContent(Util.toString(map.get("content")));
                        inquiry.setOptions(Util.toString(map.get("options")));
                        inquiry.setType(Util.toString(map.get("type")));
                        inquiry.setQusId(Util.toString(map.get("qusId")));
                        inquiries.add(inquiry);
                    }
                    view.getInquirySucced(inquiries);
                } else {
                    view.getinquirynodata();
//                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getInquiryfail();
            }
        });
    }

    void submitedcqus(String answer, String diseId, String doctorId, String hosId, String isSign) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("answer", answer);
        params.put("diseId", diseId);
        params.put("doctorId", doctorId);
        params.put("hosId", hosId);
        params.put("isSign", isSign);
        BusinessClient.post(ConstantValue.submitmdcqus, params, new MyHttpResponseHandler() {

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.getsubmitsucced(response.getString("recId"));
                } else {
                    view.showToastMessage(response.getMsg());
                    view.getsubmitfail();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getsubmitfail();
            }
        });
    }


    /**
     * 获取产品价格
     *
     * @param type
     */
    void getprdetail(String type) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        BusinessClient.post(ConstantValue.prdetail, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Prdetail prdetail = new Prdetail();
                    prdetail.setPrdId(com.dididoctor.patient.Utils.Util.toString(response.getString("prdId")));
                    prdetail.setName(com.dididoctor.patient.Utils.Util.toString(response.getString("name")));
                    prdetail.setWebUrl(com.dididoctor.patient.Utils.Util.toString(response.getString("webUrl")));
                    prdetail.setPrize((Double) response.getObject("prize") + "");
                    prdetail.setMemo(com.dididoctor.patient.Utils.Util.toString(response.getString("memo")));
                    view.getprdetail(prdetail);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getprdetailfail();
            }
        });
    }

    /**
     * 下单产品
     *
     * @param prdId
     * @param price
     * @param ptype
     * @param type
     * @param recId
     * @param doctorId
     * @param hosId
     */
    void prdbuy(String prdId, String price, final String ptype, String type, String recId, String doctorId, String hosId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("prdId", prdId);
        params.put("price", price);
        params.put("ptype", ptype);
        params.put("type", type);
        params.put("recId", recId);
        params.put("doctorId", doctorId);
        params.put("hosId", hosId);
        params.put("payWay", "1");
        BusinessClient.post(ConstantValue.prdbuy, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
//                    "payId": "S101461907984",
//                            "sign": "hxzpuMfzOktmzRTxlfyKs8nrnsGaVVAz30l9itKsa0hRIiTEXDA4sTqCsk6to/pfOHYMvT5U29lRRaUXdEL0Xp6E9rBh42Ljy5qEbj1jlRz8jeyfNmLsyErz8rlY3YPBS/DzAH2o2V/RzHF10NqEbya2swc1edWvDtQxhiM9A7A=",
//                            "_input_charset": "utf-8",
//                            "subject": "签约医生",
//                            "total_fee": "0.01",
//                            "sign_type": "RSA",
//                            "service": "mobile.securitypay.pay",
//                            "notify_url": "http://192.168.1.181:8080/doctor/pay/alipayresult.do",
//                            "partner": "2088121649593163",
//                            "seller_id": "2088121649593163",
//                            "out_trade_no": "S101461907984",
//                            "payment_type": "1"


                    Prdbuy prdbuy = new Prdbuy();
                    prdbuy.setPayId(com.dididoctor.patient.Utils.Util.toString(response.getString("payId")));
                    prdbuy.setSign(com.dididoctor.patient.Utils.Util.toString(response.getString("sign")));
                    prdbuy.setNonceStr(com.dididoctor.patient.Utils.Util.toString(response.getString("nonceStr")));
                    prdbuy.setDescr(com.dididoctor.patient.Utils.Util.toString(response.getString("descr")));
                    prdbuy.setNotifyurl(com.dididoctor.patient.Utils.Util.toString(response.getString("notifyurl")));
                    prdbuy.setTimeStamp(com.dididoctor.patient.Utils.Util.toString(response.getString("timeStamp")));
                    prdbuy.setPartnerId(com.dididoctor.patient.Utils.Util.toString(response.getString("partnerId")));
                    prdbuy.setPrepayId(com.dididoctor.patient.Utils.Util.toString(response.getString("prepayId")));
                    prdbuy.set_input_charset(com.dididoctor.patient.Utils.Util.toString(response.getString("_input_charset")));
                    prdbuy.setSubject(com.dididoctor.patient.Utils.Util.toString(response.getString("subject")));
                    prdbuy.setTotal_fee(com.dididoctor.patient.Utils.Util.toString(response.getString("total_fee")));
                    prdbuy.setSign_type(com.dididoctor.patient.Utils.Util.toString(response.getString("sign_type")));
                    prdbuy.setService(com.dididoctor.patient.Utils.Util.toString(response.getString("service")));
                    prdbuy.setNotify_url(com.dididoctor.patient.Utils.Util.toString(response.getString("notify_url")));
                    prdbuy.setPartner(com.dididoctor.patient.Utils.Util.toString(response.getString("partner")));
                    prdbuy.setSeller_id(com.dididoctor.patient.Utils.Util.toString(response.getString("seller_id")));
                    prdbuy.setOut_trade_no(com.dididoctor.patient.Utils.Util.toString(response.getString("out_trade_no")));
                    prdbuy.setPayment_type(Util.toString(response.getString("payment_type")));
                    prdbuy.setPrivateKey(Util.toString(response.getString("privateKey")));
                    view.prdbuy(prdbuy, ptype);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.prdbuyfail();
            }
        });
    }

    public void paysuccess(String type, String payId) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getId());
        params.put("token", Token.getToken());
        params.put("type", type);
        params.put("payId", payId);
        BusinessClient.post(ConstantValue.paysuccess, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.paysuccess();
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
            }
        });
    }
}
